package com.xjl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjl.constants.SystemConstants;
import com.xjl.domain.ResponseResult;
import com.xjl.domain.entity.Comment;
import com.xjl.domain.vo.CommentVo;
import com.xjl.domain.vo.PageVo;
import com.xjl.enums.AppHttpCodeEnum;
import com.xjl.exception.SystemException;
import com.xjl.mapper.CommentMapper;
import com.xjl.service.CommentService;
import com.xjl.service.UserService;
import com.xjl.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-08-08 16:16:46
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;


    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //查询对应文章的根评论

        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();

        //对articleId做判断
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);

        //根评论rootId为-1
        queryWrapper.eq(Comment::getRootId,-1);
        queryWrapper.eq(Comment::getType,commentType);

        //分页查询
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());

        //查询根评论对应的子评论集合，并且赋值给对应的属性
        for (CommentVo commentVo:commentVoList) {
            //查询对应的子评论然后赋值
            List<CommentVo> childrens = getChildren(commentVo.getId());
            commentVo.setChildren(childrens);
        }

        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }



    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(list,CommentVo.class);
        //遍历Vo集合
        for (CommentVo commentvo:commentVoList) {
            //通过createBy查询用户的昵称并复制
            String nickName = userService.getById(commentvo.getCreateBy()).getNickName();
            commentvo.setUsername(nickName);
            //通过toCommentUserId查询用户的昵称并复制
            //如果toCommentUserId不为-1才可以进行查询
            if (commentvo.getToCommentUserId()!=-1){
                String toCommentUserName = userService.getById(commentvo.getToCommentUserId()).getNickName();
                commentvo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentVoList;
    }

    @Override
    public ResponseResult addComment(Comment comment) {
//        comment.setCreateBy(SecurityUtils.getUserId());
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }


    /**
     * 根据根评论的id查询子评论的集合
     * @param id
     * @return
     */
    private List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);

        List<CommentVo> commentVoList = toCommentVoList(comments);
        return commentVoList;
    }
}
